package com.ruoyi.binding.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binding.domain.JDP.Params.JDPCusSetUserParam;
import com.ruoyi.binding.domain.JDP.Tools.OkHttpSessionClient;
import com.ruoyi.binding.domain.MhGameCategory;
import com.ruoyi.binding.service.IMhJDPService;
import com.ruoyi.binding.service.IMhSecJDPService;
import com.ruoyi.binding.service.impl.MhGameCategoryServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.binding.domain.MhBinding;
import com.ruoyi.binding.service.IMhBindingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作室改价比例Controller
 *
 * @author Ekko
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/order/binding")
public class MhBindingController extends BaseController {

    @Autowired
    private IMhBindingService mhBindingService;

    @Autowired
    private IMhSecJDPService mhJDPService;



    /**
     * 查询工作室子账户列表
     */
    @PreAuthorize("@ss.hasPermi('order:binding:list')")
    @GetMapping("/childUserList")
    public TableDataInfo childList() throws Exception {
        OkHttpSessionClient client = new OkHttpSessionClient();
        mhJDPService.login(client);
        JSONObject userList = mhJDPService.queryUserList(client);
        JSONArray jsonArray = userList.getJSONArray("data");
        List<MhBinding> mhBindingList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            MhBinding mhBinding = new MhBinding();
            JSONObject jsonObjectTemp = jsonArray.getJSONObject(i);
            mhBinding.setSuUid(jsonObjectTemp.getString("uid"));
            mhBinding.setSuUserName(jsonObjectTemp.getString("userName"));
            mhBinding.setSuLoginAcc(jsonObjectTemp.getString("loginAcc"));
            mhBinding.setSuGames(jsonObjectTemp.getString("games"));
            mhBindingList.add(mhBinding);
//            对象置空

        }
        return getDataTable(mhBindingList);
    }

    /**
     * 查询工作室改价比例列表
     */
    @PreAuthorize("@ss.hasPermi('order:binding:list')")
    @GetMapping("/list")
    public TableDataInfo list(MhBinding mhBinding) {
        startPage();
        List<MhBinding> list = mhBindingService.selectMhBindingList(mhBinding);
        return getDataTable(list);
    }



    /**
     * 导出工作室改价比例列表
     */
    @PreAuthorize("@ss.hasPermi('order:binding:export')")
    @Log(title = "工作室改价比例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MhBinding mhBinding) {
        List<MhBinding> list = mhBindingService.selectMhBindingList(mhBinding);
        ExcelUtil<MhBinding> util = new ExcelUtil<MhBinding>(MhBinding.class);
        util.exportExcel(response, list, "工作室改价比例数据");
    }

    /**
     * 获取工作室改价比例详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:binding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mhBindingService.selectMhBindingById(id));
    }

    /**
     * 根据userId获取工作室改价比例详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:binding:query')")
    @GetMapping(value = "/userId/{userId}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId) {
        return success(mhBindingService.selectMhBindingByUserId(userId));
    }

    /**
     * 新增工作室改价比例
     */
    @PreAuthorize("@ss.hasPermi('order:binding:add')")
    @Log(title = "工作室改价比例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MhBinding mhBinding) {
        logger.info("从前端接收到的工作室改价比例数据: {}", mhBinding);
        return toAjax(mhBindingService.insertMhBinding(mhBinding));
    }

    /**
     * 修改工作室改价比例
     */
    @PreAuthorize("@ss.hasPermi('order:binding:edit')")
    @Log(title = "工作室改价比例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MhBinding mhBinding) {
        return toAjax(mhBindingService.updateMhBinding(mhBinding));
    }

    /**
     * 删除工作室改价比例
     */
    @PreAuthorize("@ss.hasPermi('order:binding:remove')")
    @Log(title = "工作室改价比例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mhBindingService.deleteMhBindingByIds(ids));
    }
}
